package ru.pb.game.network.engine.client.packets.receivable;

import ru.pb.game.data.xml.holders.ChannelHolder;
import ru.pb.game.network.engine.client.packets.FromClientToGameServerPacket;
import ru.pb.game.network.engine.client.packets.sendable.PROTOCOL_SERVER_MESSAGE_ANNOUNCE_ACK;
import ru.pb.game.objects.model.CHANNEL;
import ru.pb.game.objects.model.players.Player;

public class PROTOCOL_SERVER_MESSAGE_ANNOUNCE_REQ extends FromClientToGameServerPacket
{
	private int _channelId;

	@Override
	public void readImpl()
	{
		readH();
		_channelId = readD();
	}

	@Override
	public void runImpl()
	{
		getClient().setChannelId(_channelId);

		Player p = getPlayer();
		if(p != null)
		{
			CHANNEL ch = ChannelHolder.getChannel(_channelId);

			if( !ch.getAllPlayers().contains(p.getObjectId()))
				ch.addPlayer(p);
		}
		sendPacket(new PROTOCOL_SERVER_MESSAGE_ANNOUNCE_ACK(_channelId));
	}
}
